class Solution {
    public String longestPalindrome(String ss) {
        char[] s = ss.toCharArray();
        int n = s.length;
        int begin = 0, end = 0;
        // 固定一个中心点
        for (int i = 0; i < n; i++) {
            // 从中心点开始往两侧扩展
            // 先扩展奇数个
            int left = i, right = i;
            while (left >= 0 && right <= n-1 && s[left] == s[right]) {
                left--;
                right++;
            }
            // 更新begin 与 end
            if (right-left-1 > end-begin+1) {
                begin = left+1;
                end = right-1;
            }
            // 再扩展偶数个
            left = i;
            right = i+1;
            while (left >= 0 && right <= n-1 && s[left] == s[right]) {
                left--;
                right++;
            }
            //更新begin 与 end
            if (right-left-1 > end-begin+1) {
                begin = left+1;
                end = right-1;
            }
        }
        return ss.substring(begin, end+1);
    }
}